package cn.sym.org.controller;
import cn.sym.basic.util.AjaxResult;
import cn.sym.basic.util.PageList;
import cn.sym.org.domain.Department;
import cn.sym.org.query.DepartmentQuery;
import cn.sym.org.service.IDepartmentService;
import cn.sym.system.anno.PreAuthorize;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService  iDepartmentService;
    /**
     * 添加和修改
     */
    //@Operation(value = "新增和修改")
    @PutMapping
    public AjaxResult addAndUpdate(@RequestBody Department department){
        System.out.println(department);
        try {
            if (department.getId()==null){
                iDepartmentService.add(department);
            }else{
                iDepartmentService.update(department);
            }
            return new AjaxResult();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("操作失败！！！");
        }
    }

    /**
     * 删除
     */
   // @Operation("单条删除")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            iDepartmentService.remove(id);
            return AjaxResult.my();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("网络错误，请稍后再试！！1");
        }
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
   // @Operation("批量删除")
    @PatchMapping
    public AjaxResult remove(@RequestBody List<Long> ids){
        try {
            iDepartmentService.removeMore(ids);
            return AjaxResult.my();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.my().setSuccess(false).setMessage("网络错误，请稍后再试！！1");
        }
    }
    /**
     * 通过id查询
     */
   // @Operation("通过id查询")
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return iDepartmentService.queryById(id);
    }
    /**
     * 查询所有
     */
   // @Operation("查询所有")
    @GetMapping
    public List<Department> queryAll(){
        return iDepartmentService.queryAll();
    }
    /**
     * 查询无限极部门树
     * @return
     */
   // @Operation("无限极部门树")
    @GetMapping("/tree")
    public List<Department> queryTree(){
        return iDepartmentService.queryTree();
    }
    /**
     * 高级查询
     */
   // @Operation("高级查询加分页")
    @PreAuthorize(name = "部门列表",value = "department:list")
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
        return iDepartmentService.queryPage(query);
    }

}
